var util = require('../../utils/util.js')
var app = getApp();
var md5 = require('../md5.js');
Page({
  data: {
    currentTab: 0,
    showModal: false,
    isallchecked:false,
    page: 1,
    recommend: 0
  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 1
    })
    // 页面初始化 options为页面跳转所带来的参数 
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Batch selection'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '批量选择'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    // 上拉刷新
    if (!that.loading) {
      wx.showToast({
        title: '数据刷新中',
        icon: 'success',
        duration: 1000
      })
      //获取缓存展会id和用户id存在则获取最新联系人列表
      var exhibitionid = wx.getStorageSync('exhibitionid')
      var u = wx.getStorageSync('u')
      console.log('exhibitionid=', exhibitionid)
      console.log('u=', u)
      if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
        && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
        //默认最新（调用pico接口）
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        var url = app.globalData.picoaddress + '/attendee/contacts/list'
        var k = {}
        k["v"] = '1.0.0'
        k["client"] = 'ios'
        k["timestamp"] = timestamp
        k["lan"] = 'cn'
        k["exhibitionid"] = exhibitionid
        k["u"] = u
        k["isrecommend"] = '0'
        var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0timestamp' + timestamp + app.globalData.picotoken
        console.log(sign)
        var curb64 = md5.hexMD5(sign)
        console.log(curb64)
        k["sign"] = curb64
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: url,
          method: 'post',
          data: k,
          success(res) {
            console.log(res)
            var mesg = res.data
            var curdata = mesg.data
            for (var index in curdata) {
              curdata[index].ischecked = false
            }
            console.log('curdata=' + curdata)
            that.setData({
              personMesg: curdata
            });
          }, fail: res => {
            console.log(res, "error")
          }

        })
      } else {
        wx.redirectTo({
          url: '../signInbyPhone/signIn'
        })
      }
      // 处理完成后，终止下拉刷新
      wx.stopPullDownRefresh()
    }
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  preventTouchMove: function () {

  },

  go: function (e) {
    var that = this;
    that.setData({
      page: 1
    })
    var kk = that.data.currentTab
    console.log("curtab=", kk)
    //获取关键词
    var keyword = e.detail.value
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('keyword=' + keyword)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/contacts/list'
      var k = {}
      var sign = ''
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["isrecommend"] = '0'
     
      if (keyword || !"".trim() == keyword) {
        k["keyword"] = keyword
        k["page"] = 0
        sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0' + 'keyword' + keyword + 'page0'+'timestamp' + timestamp + app.globalData.picotoken
      } else {
        sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0timestamp' + timestamp + app.globalData.picotoken
      }
      console.log(sign)
      var curb64 = md5.zhMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          var curdata = mesg.data
          for (var index in curdata) {
            curdata[index].ischecked = false
          }
          console.log('curdata=' + curdata)
          that.setData({
            personMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },

  onReachBottom() {
    // 下拉触底，先判断是否有请求正在进行中
    var that = this
    //滚动条回顶部
    // wx.pageScrollTo({
    //   scrollTop: 0,
    //  })
    var page = that.data.page + 1
    that.setData({
      page: page
    })
    if (!that.loading) {
      wx.showToast({
        title: '下拉加载',
        icon: 'success',
        duration: 1000
      })
      //获取缓存展会id和用户id存在则获取最新联系人列表
      var exhibitionid = wx.getStorageSync('exhibitionid')
      var u = wx.getStorageSync('u')
      console.log('exhibitionid=', exhibitionid)
      console.log('u=', u)
      var recommend = that.data.recommend
      if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
        && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
        //默认最新（调用pico接口）
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        var url = app.globalData.picoaddress + '/attendee/contacts/list'
        var k = {}
        k["v"] = '1.0.0'
        k["client"] = 'ios'
        k["timestamp"] = timestamp
        k["lan"] = 'cn'
        k["exhibitionid"] = exhibitionid
        k["u"] = u
        k["isrecommend"] = '0'
        k["recommend"] = recommend
        k["page"] = page

        var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0page' + page + 'recommend' + recommend + 'timestamp' + timestamp + app.globalData.picotoken
        console.log(sign)
        var curb64 = md5.zhMD5(sign)
        console.log(curb64)
        k["sign"] = curb64
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: url,
          method: 'post',
          data: k,
          success(res) {
            console.log(res)
            var mesg = res.data
            console.log('mesg=' + mesg)
            var curdata = mesg.data
            var curarray = []
            var oldpersonMesg = that.data.personMesg
            for (var i = 0; i < oldpersonMesg.length; i++) {
              curarray.push(oldpersonMesg[i])
            }
            for (var i = 0; i < curdata.length; i++) {
              curarray.push(curdata[i])
            }
            that.setData({
              personMesg: curarray
            })

          }, fail: res => {
            console.log(res, "error")
          }
        })

      } else {
        wx.redirectTo({
          url: '../signInbyPhone/signIn'
        })
      }
    }
  },

  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数 
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Batch selection'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '批量选择'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=', exhibitionid)
    console.log('u=', u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/contacts/list'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["u"] = u
      k["isrecommend"] = '0'
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'isrecommend0timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          var curdata = mesg.data
          for (var index in curdata){
            curdata[index].ischecked =false
          }
          console.log('curdata=' + curdata)
          that.setData({
            personMesg: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })
    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }

  },
  itemSelected:function(e){
    var that = this
    var index = e.currentTarget.dataset.index
    var item = that.data.personMesg[index]
    item.ischecked = !item.ischecked
    that.setData({
      personMesg: that.data.personMesg
    })
  },
  allSelected:function(e){
   var that = this
    var curisallchecked = that.data.isallchecked
    if (curisallchecked){
      var curdata = that.data.personMesg
      for (var index in curdata) {
        curdata[index].ischecked = false
      }
      that.setData({
        personMesg: curdata,
        isallchecked: false
      })
    }else{
      var curdata = that.data.personMesg
      for (var index in curdata) {
        curdata[index].ischecked = true
      }
      that.setData({
        personMesg: curdata,
        isallchecked: true
      })
    }
  },
  exportEmail: function (e) {
    var that = this
    var curdata = that.data.personMesg
    var attendeeids =''
    for (var index in curdata) {
      if (curdata[index].ischecked){
        attendeeids = curdata[index].attendeeid+','
       }
     }
    if (attendeeids==''){
      //获取缓存展会id和用户id存在则获取最新联系人列表
      var exhibitionid = wx.getStorageSync('exhibitionid')
      var u = wx.getStorageSync('u')
      console.log('exhibitionid=', exhibitionid)
      console.log('u=', u)
      if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
        && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
        //默认最新（调用pico接口）
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        var url = app.globalData.picoaddress + '/attendee/contacts/export'
        var k = {}
        k["v"] = '1.0.0'
        k["client"] = 'ios'
        k["timestamp"] = timestamp
        k["lan"] = 'cn'
        k["exhibitionid"] = exhibitionid
        k["u"] = u
        var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
        console.log(sign)
        var curb64 = md5.hexMD5(sign)
        console.log(curb64)
        k["sign"] = curb64
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: url,
          method: 'post',
          data: k,
          success(res) {
            console.log(res)   
            wx.showToast({
              title: '导出成功！',
              icon: 'success',
              duration: 2000
            })        
          }, fail: res => {
            console.log(res, "error")
          }
        })
      } else {
        wx.redirectTo({
          url: '../signInbyPhone/signIn'
        })
      }
     }else{
      attendeeids = attendeeids.substr(0, attendeeids.length-1)
      console.log('attendeeids=' + attendeeids)
      //获取缓存展会id和用户id存在则获取最新联系人列表
      var exhibitionid = wx.getStorageSync('exhibitionid')
      var u = wx.getStorageSync('u')
      console.log('exhibitionid=', exhibitionid)
      console.log('u=', u)
      if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
        && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
        //默认最新（调用pico接口）
        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        var url = app.globalData.picoaddress + '/attendee/contacts/export'
        var k = {}
        k["v"] = '1.0.0'
        k["client"] = 'ios'
        k["timestamp"] = timestamp
        k["lan"] = 'cn'
        k["exhibitionid"] = exhibitionid
        k["attendeeids"] = attendeeids
        k["u"] = u
        var sign = app.globalData.picotoken + 'attendeeids' + attendeeids+ 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
        console.log(sign)
        var curb64 = md5.hexMD5(sign)
        console.log(curb64)
        k["sign"] = curb64
        wx.request({
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          url: url,
          method: 'post',
          data: k,
          success(res) {
            console.log(res)
            wx.showToast({
              title: '导出成功！',
              icon: 'success',
              duration: 2000
            })  
          }, fail: res => {
            console.log(res, "error")
          }
        })
      } else {
        wx.redirectTo({
          url: '../signInbyPhone/signIn'
        })
      }
     }
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('myMap')
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function (res) {
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  }
})